package org.ns.framework.datasource.mybatis;

import java.util.LinkedList;

/**
 * 事务链
 * 
 * @author zhangxingyu
 *
 */
public class MybatisTransactionChain {

	private final static ThreadLocal<LinkedList<MybatisTransactionReference>> threadLocal = new ThreadLocal<>();

	public static <T extends MybatisTransactionReference> T get() {
		LinkedList<MybatisTransactionReference> linkedList = threadLocal.get();
		if (linkedList == null || linkedList.size() == 0) {
			return null;
		}
		return (T) linkedList.getFirst();
	}

	public static void set(MybatisTransactionReference transactionManage) {
		LinkedList<MybatisTransactionReference> linkedList = threadLocal.get();
		if (linkedList == null) {
			linkedList = new LinkedList<>();
			threadLocal.set(linkedList);
			;
		}
		linkedList.addFirst(transactionManage);
	}

	public static void remove() {
		LinkedList<MybatisTransactionReference> linkedList = threadLocal.get();
		if (linkedList == null || linkedList.size() == 0) {
			return;
		}
		linkedList.removeFirst();
	}

}
